function [labeledI] = kmeansColor(I, nColors, space)
%KMEANSCOLOR Summary of this function goes here
%   Detailed explanation goes here

warning off

if strcmp(space,'lab')
    cform = makecform('srgb2lab');
    lab_he = applycform(I,cform);

    ab = double(lab_he(:,:,2:3));
    nrows = size(ab,1);
    ncols = size(ab,2);
    ab = reshape(ab,nrows*ncols,2);

    [cluster_idx cluster_center] = kmeans(ab,nColors,'distance','sqEuclidean','Replicates', 3,'emptyaction','drop');
    labeledI = reshape(cluster_idx,nrows,ncols);
    
elseif strcmp(space,'rgb')
    [m n ~] = size(I);
    rgb = double(reshape(I,m*n,3));
    [cluster_idx cluster_center] = kmeans(rgb,nColors,'distance','sqEuclidean','Replicates',3,'emptyaction','drop');
    labeledI = reshape(cluster_idx,m,n);

elseif strcmp(space,'intensity')
    
    [cluster_idx cluster_center] = kmeans(I(:),nColors,'distance','sqEuclidean','Replicates',3,'emptyaction','drop');
    labeled = reshape(cluster_idx,size(I));
    
elseif strcmp(space,'none')
    
    [cluster_idx cluster_center] = kmeans(I,nColors,'distance','sqEuclidean','Replicates',3,'emptyaction','drop');
    labeledI = cluster_idx;
    
end

warning on
end

